import {
  getListStrategyPro
} from '@/api/quant'
import {
  distributionEntrust3,
} from '@/api/trade'
import {
  TimeToDate,
  isPositiveInteger,
} from '@/libs/tools'
import base from "../../api/base";
export default {
  // namespaced:true,
  state: {
    list_strategy:[],//量化策略列表
    list_strategy_unrun:[], //当前用户未启动量化策略
    list_strategy_run:[], //当前用户启动量化策略
    data_group_id_run:[],//启动策略组别
    data_group_id_unrun:[],//未启动策略组别
    run_group:'',//启动的组号
    unrun_group:'',//未启动的组号
    Interval_getListStrategy:'',//定时请求策略
    strategy_data:[],//委托分布图数据
    Interval_get_distributionEntrust:'',//定时请求委托分布图数据
    all_group:'',//已启动和未启动分组
    strategy_time:{
      start_time:'',
      end_time:'',
    }
  },
  mutations: {
    set_list_strategy(state, list_strategy) {
      if(JSON.stringify(state.list_strategy) !== JSON.stringify(list_strategy)){
        state.list_strategy = JSON.parse(JSON.stringify(list_strategy))
      }
    },
    set_list_strategy_unrun(state, list_strategy_unrun) {
      if(JSON.stringify(state.list_strategy_unrun) !== JSON.stringify(list_strategy_unrun)){
        state.list_strategy_unrun = JSON.parse(JSON.stringify(list_strategy_unrun))
      }
    },
    set_list_strategy_run(state, list_strategy_run) {
      if(JSON.stringify(state.list_strategy_run) !== JSON.stringify(list_strategy_run)){
        state.list_strategy_run = JSON.parse(JSON.stringify(list_strategy_run))
      }
    },
    set_data_group_id_run(state, data_group_id_run) {
      if(JSON.stringify(state.data_group_id_run) !== JSON.stringify(data_group_id_run)){
        state.data_group_id_run = JSON.parse(JSON.stringify(data_group_id_run))
      }
    },
    set_data_group_id_unrun(state, data_group_id_unrun) {
      if(JSON.stringify(state.data_group_id_unrun) !== JSON.stringify(data_group_id_unrun)){
        state.data_group_id_unrun = JSON.parse(JSON.stringify(data_group_id_unrun))
      }
    },

    set_run_group(state, run_group) {
      if(JSON.stringify(state.run_group) !== JSON.stringify(run_group)){
        state.run_group = JSON.parse(JSON.stringify(run_group))
      }
    },
    set_unrun_group(state, unrun_group) {
      if(JSON.stringify(state.unrun_group) !== JSON.stringify(unrun_group)){
        state.unrun_group = JSON.parse(JSON.stringify(unrun_group))
      }
    },
    set_strategy_data(state, strategy_data) {
      if(JSON.stringify(state.strategy_data) !== JSON.stringify(strategy_data)){
        state.strategy_data = JSON.parse(JSON.stringify(strategy_data))
      }
    },
    set_all_group(state, all_group) {
      if(JSON.stringify(state.all_group) !== JSON.stringify(all_group)){
        state.all_group = JSON.parse(JSON.stringify(all_group))
      }
    },
    set_strategy_time(state, strategy_time) {
      if(JSON.stringify(state.strategy_time) !== JSON.stringify(strategy_time)){
        state.strategy_time = JSON.parse(JSON.stringify(strategy_time))
      }
    },
  },
  getters: {
    get_list_strategy:state =>state.list_strategy,
    get_list_strategy_run:state =>state.list_strategy_run,
    get_list_strategy_unrun:state =>state.list_strategy_unrun,
    get_data_group_id_run:state =>state.data_group_id_run,
    get_data_group_id_unrun:state =>state.data_group_id_unrun,
    get_run_group:state =>state.run_group,
    get_unrun_group:state =>state.unrun_group,
    get_Interval_getListStrategy:state =>state.Interval_getListStrategy,
    get_strategy_data:state =>state.strategy_data,
    get_all_group:state =>state.all_group,
    get_strategy_time:state =>state.strategy_time,
  },
  actions: {
    //获取策略
    //获取策略
    getListStrategy({ commit }, {client,contract}){
      return new Promise((resolve, reject) => {
        getListStrategyPro({client,contract}).then(res => {
          const data = res.data;
          if(data.code == 1000){
            if(data.data_group_id_run !==undefined){
              commit('set_list_strategy_run', data.strategy_list_run
                .map( function(current, index, array){
                  current.open = Number(current.open);
                  current.close = Number(current.close);
                  if (current.create_time !== '' && current.create_time !== 'None') {
                    current.create_time = TimeToDate(current.create_time, 'year');
                  }
                  if (current.points_translation_time !== '' && current.points_translation_time !== 'None') {
                    current.points_translation_time = TimeToDate(current.points_translation_time, 'year');
                  }
                  if (current.stop_time_time_or_len === 'len' && current.stop_time_len !== '' && current.stop_time_len !== 'None') {
                    current.stop_len = current.stop_time_len;
                    current.stop_time = 'None'
                  }
                  if (current.stop_time_time_or_len === 'time' && current.stop_time !== '' && current.stop_time !== 'None') {
                    current.stop_time = TimeToDate(current.stop_time, 'year');
                    current.stop_len = 'None';
                  }
                  if (current.stop_time_time_or_len === 'None') {
                    current.stop_time = 'None';
                    current.stop_len = 'None';
                  }
                  return current;
                }));
              commit('set_list_strategy_unrun', data.strategy_list_unrun
                .map( function(current, index, array){
                  current.open = Number(current.open);
                  current.close = Number(current.close);
                  if (current.create_time !== '' && current.create_time !== 'None') {
                    current.create_time = TimeToDate(current.create_time, 'year');
                  }
                  if (current.points_translation_time !== '' && current.points_translation_time !== 'None') {
                    current.points_translation_time = TimeToDate(current.points_translation_time, 'year');
                  }
                  if (current.stop_time_time_or_len === 'len' && current.stop_time_len !== '' && current.stop_time_len !== 'None') {
                    current.stop_len = current.stop_time_len;
                    current.stop_time = 'None'
                  }
                  if (current.stop_time_time_or_len === 'time' && current.stop_time !== '' && current.stop_time !== 'None') {
                    current.stop_time = TimeToDate(current.stop_time, 'year');
                    current.stop_len = 'None';
                  }
                  if (current.stop_time_time_or_len === 'None') {
                    current.stop_time = 'None';
                    current.stop_len = 'None';
                  }
                  return current;
                }));
              commit('set_data_group_id_run', data.data_group_id_run);
              commit('set_data_group_id_unrun', data.data_group_id_unrun);
              commit('set_run_group', data.list_group_id_run);
              commit('set_unrun_group', data.list_group_id_unrun);
              commit('set_all_group', [...new Set([...data.list_group_id_run ,...data.list_group_id_unrun])]);//两数组合并去重复
              resolve("成功获取策略")
            }else {
              commit('set_list_strategy', []);
              commit('set_list_strategy_run', []);
              commit('set_list_strategy_unrun', []);
              commit('set_data_group_id_run',[]);
              commit('set_data_group_id_unrun', []);
              commit('set_all_group', []);
              commit('set_run_group',[]);
              commit('set_unrun_group',[]);
              resolve("没有策略")
            }
          }else{
            reject(data.data.msg)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    //定时获取策略
    getListStrategypro({ state,dispatch }, {client,contract}){
      clearInterval(state.Interval_getListStrategy );
      state.Interval_getListStrategy = setInterval(() =>{dispatch('getListStrategy', {client,contract})}, 5000);
      return dispatch('getListStrategy', {client,contract});
    },
    //获取委托分布图
    get_distributionEntrust({ state,commit }, {client,contract}){
      return new Promise((resolve, reject) => {
        distributionEntrust3({client,contract}).then(res => {
          const data = res.data;
          if(data.code == 1000){
            let new_data = []
            // let price_ref = 0
            if(data.data.length>0){
              // console.log(data.data)
              for(let i = 0;i<data.data.length;i++){
                if(i === 0 || i===data.data.length){
                  //给最上面加一个
                  new_data.push({
                    'strategy_id': 0,
                    'price': Math.round(data.data[i]['price']) - 1,
                    'value': 0,
                    'group_id': '0',
                    'color_selected': '#A4FF68',
                    'color_not_selected': '#5F8F50'}
                  )
                  new_data.push(data.data[i])
                  // price_ref = data.data[i]['price']
                }else{
                  // while(data.data[i]['price'] > price_ref + 1) {
                  //   new_data.push({
                  //     'strategy_id': 0,
                  //     'price': Math.round(price_ref) + 1,
                  //     'value': 0,
                  //     'group_id': '0',
                  //     'color_selected': '#A4FF68',
                  //     'color_not_selected': '#5F8F50'}
                  //     )
                  //   price_ref = price_ref + 1
                  // }
                  new_data.push(data.data[i])
                  // price_ref = data.data[i]['price']
                }
              }
            //给最下面加一个
            //   new_data.push({
            //     'strategy_id': 0,
            //     'price': Math.round(price_ref) + 1,
            //     'value': 0,
            //     'group_id': '0',
            //     'color_selected': '#A4FF68',
            //     'color_not_selected': '#5F8F50'})
            // console.log(new_data)
            }
            if(data.status ===0){
              clearInterval(state.Interval_get_distributionEntrust);
            }
            commit('set_strategy_data', new_data);
            resolve('成功获取委托分布图数据')
          }else{
            clearInterval(state.Interval_get_distributionEntrust);
            reject(data.data.msg)
          }
        }).catch(err => {
          clearInterval(state.Interval_get_distributionEntrust);
          reject(err)
        })
      })
    },
    //定时获取委托分布图
    getDistributionEntrustPro({ state,dispatch }, {client,contract}){
      clearInterval(state.Interval_get_distributionEntrust );
      state.Interval_get_distributionEntrust = setInterval(() =>{dispatch('get_distributionEntrust', {client,contract})}, 5000);
      return dispatch('get_distributionEntrust', {client,contract});
    },
    stopDistributionEntrust({ state,dispatch }){
      clearInterval(state.Interval_get_distributionEntrust );
    },
    quant_logOut({ state, commit }){
      clearInterval(state.Interval_getListStrategy );
      clearInterval(state.Interval_get_distributionEntrust );
      commit('set_list_strategy',[]);
      commit('set_data_group_id_run',[]);
      commit('set_data_group_id_unrun',[]);
      commit('set_run_group',[]);
      commit('set_unrun_group',[]);
      commit('set_list_strategy_run', []);
      commit('set_list_strategy_unrun', []);
      commit('set_all_group', []);
    }
  },

}
